package com.l.codegen;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * Hello world!
 *
 */
public class CodeGenerator {

    private static final String auth = "严秋旺";
    
    private static final String jdbcUrl = "jdbc:mysql://localhost:3306/lcourse?useUnicode=true&useSSL=false&characterEncoding=utf8";
    private static final String jdbcDriver = "com.mysql.jdbc.Driver";
    private static final String jdbcUsername = "root";
    private static final String jdbcPasword = "123456";

    private static final String packageParent = "com.l.web.master";
    private static final String packageModuleName = "video";
    
    private static final String tableName = "video_content";
    
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = "build";
        gc.setOutputDir(projectPath + "/java");
        gc.setAuthor(auth);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setOpen(false);
        gc.setActiveRecord(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(jdbcUrl);
        // dsc.setSchemaName("public");
        dsc.setDriverName(jdbcDriver);
        dsc.setUsername(jdbcUsername);
        dsc.setPassword(jdbcPasword);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageParent);
        pc.setModuleName(packageModuleName);
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/resources/mapper/" + pc.getModuleName() + "/"
                        + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        focList.add(new FileOutConfig("/templates/index.ftl.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/webapp/WEB-INF/ftl/master/" + pc.getModuleName() +"/" + tableInfo.getEntityPath() + "/index.ftl";
            }
        });
        focList.add(new FileOutConfig("/templates/edit.ftl.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/webapp/WEB-INF/ftl/master/" + pc.getModuleName() + "/" + tableInfo.getEntityPath() + "/edit.ftl";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("com.baomidou.mybatisplus.activerecord.Model");
        strategy.setEntityLombokModel(true);
//        strategy.setRestControllerStyle(true);
//		strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude(tableName);
//		strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setControllerMappingHyphenStyle(false);
        
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("del", FieldFill.INSERT));
        tableFillList.add(new TableFill("create_by", FieldFill.INSERT));
        tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_by", FieldFill.UPDATE));
        tableFillList.add(new TableFill("update_time", FieldFill.UPDATE));
        strategy.setTableFillList(tableFillList);
        
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerGeneratorEngine());
        mpg.execute();
    }

}
